const app = getApp(),url = app.globalData.url;
Page({
  data: {
    length: 0,
    num:500,
    introduction:'',
    cvBaseInfo:{}
  },

  onLoad (options) {
    var that = this;
    that.setData({ id:options.id });
    var uid = wx.getStorageSync('user_massage')
    if(options.id > 0){
      wx.request({
        url: url + 'cvBaseInfo/getCVBaseInfoByUserUid?user_uid='+uid.userUid,
        method: "GET",
        success(res){
          var ret = res.data
          if(ret.code == 200){
            that.setData({
              length: ret.data.introduction ? ret.data.introduction.length : 0,
              introduction: ret.data.introduction,
              cvBaseInfo: ret.data
            })
          }else{
            wx.showToast({
              title: ret.msg,
              icon:'none'
            })
          }
        }
      })
    }
  },
  // 评价
  userInput: function(e) {
    this.setData({
      length: e.detail.value.length,
      introduction: e.detail.value
    })
  },
  
  checkSensWords(e){
    let itemData = e.target.dataset;
    let label = Object.values(itemData)[0]
    let key = Object.keys(itemData)[0]
    this.sensWords = this.sensWords || {}
    app.validSensWords(e.detail.value, label).then(() => {
      delete this.sensWords[key]
    }).catch(() => {
      this.sensWords = {...itemData, ...this.sensWords}
    })
  },
  savedata(){
    var that = this
    let sensWords = Object.values(this.sensWords  || {})
    if(sensWords.length > 0){
      wx.showToast({
        title: `${sensWords.join(',')}包含敏感词，请修改后再保存`,
        icon:'none',
      })
			return false;
    }
    that.setData({sub:false})
    var uid = wx.getStorageSync('user_massage')
    
    var urlapi = "cvBaseInfo/update"
    var mothed = "PUT"
    let data = this.data.cvBaseInfo
    data.approval = '1'
    data.introduction = this.data.introduction
    
    wx.request({
      url:url+urlapi,
      header:{ 'context-type': 'application/json' },
      method: mothed,
      data: data,
      success(res){
        var ret = res.data
        if(ret.code == 200){
          setTimeout(function(){
            wx.navigateBack({
              delta: 1,
            })
          },1000)
          
        }else{
          that.setData({sub:true})
          wx.showToast({
            title:'保存失败',
            icon:'none',
          })
        }
      }
    })
  },
})